Skip to content

Optmize email normalization #6557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions kitsune/sumo/email_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from post_office.settings import get_override_recipients
from premailer import transform


log = logging.getLogger("k.email")


Expand All @@ -31,34 +30,28 @@ def normalize_gmail(email: str) -> str:
return email


def is_valid_email(email: str) -> bool:
"""
Returns True if the given email address is valid, False otherwise.
"""
try:
validate_email(normalize_gmail(email))
except ValidationError:
return False
return True


def send_messages(messages):
"""Sends a bunch of email messages."""
if not messages:
return

# Only send each message to its valid recipients,
# excluding messages without any valid recipients.
cleaned_messages = []
valid_messages = []
for message in messages:
# Remove invalid emails and normalize gmails.
cleaned_to = [normalize_gmail(email) for email in message.to if is_valid_email(email)]
if cleaned_to:
message.to = cleaned_to
cleaned_messages.append(message)
valid_emails = []
for email in message.to:
normalized_email = normalize_gmail(email)
try:
validate_email(normalized_email)
valid_emails.append(normalized_email)
except ValidationError:
pass

if valid_emails:
message.to = valid_emails
valid_messages.append(message)

with mail.get_connection(fail_silently=True) as conn:
conn.send_messages(cleaned_messages)
conn.send_messages(valid_messages)


def safe_translation(f):
Expand Down
19 changes: 18 additions & 1 deletion kitsune/sumo/tests/test_email_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.mail import EmailMultiAlternatives
from django.utils.functional import lazy
from django.utils import translation
from django.utils.functional import lazy
from django.utils.translation import get_language

from kitsune.sumo.email_utils import emails_with_users_and_watches, safe_translation, send_messages
Expand Down Expand Up @@ -156,3 +156,20 @@ def test_send_messages(self, mock_mail):
self.assertEqual(messages[1].to, ["georgeharrison@gmail.com"])
self.assertEqual(messages[2].to, ["paulmccartney@gmail.com"])
self.assertEqual(messages[3].to, ["ringo@beatles.com", "george@beatles.com"])

@patch("kitsune.sumo.email_utils.mail")
def test_send_messages_with_invalid_email(self, mock_mail):
from_email = "notifications@support.mozilla.org"
messages = [
EmailMultiAlternatives(
"Test",
"Testing",
from_email,
["valid@example.com", "invalid@email", "another.valid@example.com"],
),
]
send_messages(messages)
send_messages_mock = mock_mail.get_connection().__enter__().send_messages
send_messages_mock.assert_called_once_with(messages)
# Check that the invalid email was removed
self.assertEqual(messages[0].to, ["valid@example.com", "another.valid@example.com"])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another.valid@example.com is a valid email address. invalid.@example.com is not. The need for the gmail normalization is because for gmail addresses with dots (.) are the same. This is not true for other systems

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the original test above did test an invalid email address ("ringo").